package org.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.constant.ArticleStatusEnum;
import org.chen.common.constant.VideoStatusEnum;
import org.chen.common.exception.ServiceException;
import org.chen.domain.dto.AvatarQueryDTO;
import org.chen.domain.entity.VideoArticle;
import org.chen.domain.entity.VideoAvatar;
import org.chen.domain.response.PageResult;
import org.chen.domain.vo.ArticleVO;
import org.chen.domain.vo.AvatarVO;
import org.chen.mapper.AvatarMapper;
import org.chen.service.AvatarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AvatarServiceImpl extends ServiceImpl<AvatarMapper,VideoAvatar> implements AvatarService {
    
    @Autowired
    private AvatarMapper avatarMapper;
    
    @Override
    public PageResult<AvatarVO> getAvatarList(AvatarQueryDTO avatarQueryDTO) {
        Page<VideoAvatar> page = new Page<>(avatarQueryDTO.getPageNum(), avatarQueryDTO.getPageSize());

        LambdaQueryWrapper<VideoAvatar> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(avatarQueryDTO.getGender())) {
            wrapper.eq(VideoAvatar::getGender, avatarQueryDTO.getGender());
        }
        
        if (avatarQueryDTO.getPremium() != null) {
            wrapper.eq(VideoAvatar::getPremium, avatarQueryDTO.getPremium());
        }
        
        wrapper.orderByAsc(VideoAvatar::getAvatarName);

        Page<VideoAvatar> resultPage = avatarMapper.selectPage(page, wrapper);

        List<AvatarVO> voList = resultPage.getRecords().stream()
                .map(this::convertToVO)
                .toList();

        return new PageResult<>(voList,
                resultPage.getTotal(),
                resultPage.getSize(),
                resultPage.getCurrent()
        );
    }
    
    @Override
    public Map<String, Long> getAvatarStats() {
        List<VideoAvatar> avatars = avatarMapper.selectList(null);

        // 按性别统计
        Map<String, Long> genderStats = avatars.stream()
            .collect(Collectors.groupingBy(
                VideoAvatar::getGender,
                Collectors.counting()
            ));
        Map<String, Long> stats = new HashMap<>(genderStats);
        
        // 按高级版本统计
        long premiumCount = avatars.stream()
            .filter(VideoAvatar::getPremium)
            .count();

        // 按基础版统计
        long basicCount = avatars.stream()
                .filter(avatar -> !avatar.getPremium())
                .count();

        stats.put("premium", premiumCount);
        stats.put("basic", basicCount);

        return stats;
    }

    @Override
    public VideoAvatar getAvatarById(String id) {
        LambdaQueryWrapper<VideoAvatar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VideoAvatar::getAvatarId, id);
        VideoAvatar avatar = avatarMapper.selectOne(wrapper);
        if (avatar == null) {
            throw new ServiceException("虚拟角色不存在");
        }
        return avatar;
    }

    private AvatarVO convertToVO(VideoAvatar avatar) {
        AvatarVO vo = new AvatarVO();
        BeanUtils.copyProperties(avatar, vo);
        return vo;
    }
}